/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.restclient.utils;

import cz.muni.fi.pa165.machineryrental.dto.CustomerDTO;
import cz.muni.fi.pa165.machineryrental.dto.CustomerType;
import cz.muni.fi.pa165.machineryrental.dto.MachineDTO;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpHeaders;

/**
 *
 * @author David Katuscak, 359273
 */
public class Utils {

    private static SimpleDateFormat czechSDF = new SimpleDateFormat("dd.MM.yyyy");

    public static HttpHeaders createHeaders(final String username, final String password) {
        return new HttpHeaders() {
            {
                String auth = username + ":" + password;
                byte[] encodedAuth = Base64.encodeBase64(
                        auth.getBytes(Charset.forName("US-ASCII")));
                String authHeader = "Basic " + new String(encodedAuth);
                set("Authorization", authHeader);
            }
        };
    }

    public static HttpHeaders createJSONHeaders(final String username, final String password) {
        HttpHeaders headers = createHeaders(username, password);

        headers.add("Content-Type", "application/json;charset=UTF-8");
        return headers;
    }

    public static void clearTable(JTable table) {

        DefaultTableModel model = (DefaultTableModel) table.getModel();
        int count = model.getRowCount();
        if (count > 0) {
            for (int i = 0; i < count; i++) {
                model.removeRow(0);
            }
        }
    }

    public static MachineDTO toDtos(LinkedHashMap map) throws ParseException {

        String brand = (String) map.get("brand");
        String code = (String) map.get("code");
        String model = (String) map.get("model");
        String year = (String) map.get("yearOfProduction");
        Integer id = (Integer) map.get("id");

        Date yearOfProduction;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        yearOfProduction = sdf.parse(year);


        MachineDTO machine = new MachineDTO();
        machine.setId(new Long(id));
        machine.setBrand(brand);
        machine.setCode(code);
        machine.setModel(model);
        machine.setYearOfProduction(yearOfProduction);

        return machine;
    }

    public static CustomerDTO toCustomerDto(LinkedHashMap map) throws ParseException {

        int id = (Integer) map.get("id");
        String type = (String) map.get("type");
        CustomerType custType;
        if (type.equals("PERSONAL")) {
            custType = CustomerType.PERSONAL;
        } else {
            custType = CustomerType.BUSINESS;
        }

        CustomerDTO customer = new CustomerDTO();
        customer.setId(id);
        customer.setFirstName((String) map.get("firstName"));
        customer.setLastName((String) map.get("lastName"));
        customer.setEmail((String) map.get("email"));
        customer.setPhone((String) map.get("phone"));
        customer.setStreet((String) map.get("street"));
        customer.setCity((String) map.get("city"));
        customer.setZipCode((String) map.get("zipcode"));
        customer.setCountry((String) map.get("country"));
        customer.setCustomerType(custType);
        customer.setVatNumber((String) map.get("vatNumber"));
        customer.setCompanyNumber((String) map.get("companyNumber"));

        return customer;
    }

    public static Object[] toRow(MachineDTO m) {

        String date = Utils.czechSDF.format(m.getYearOfProduction());
        return new Object[]{m.getBrand(), m.getModel(), m.getCode(), date};
    }

    public static Object[] toRow(CustomerDTO c) {

        return new Object[]{
            c.getFirstName(),
            c.getLastName(),
            c.getEmail(),
            c.getPhone(),
            c.getStreet() + ", " + c.getZipCode() + " " + c.getCity() + " " + c.getCountry(),
            c.getCustomerType(),
            c.getVatNumber(),
            c.getCompanyNumber()
        };
    }
}
